$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;

    //点击返回箭头 清除 缓存 打开 storekeeper.html
    $(".head .goBack").on("click", function () {
        window.location.href = "storekeeperRope.html";
    });

    $("#goods_id").focus();

    //点击 刷新 按钮  重新加载页面
    $(".page .head").on("click", "span.icon-refresh-1-copy", function () {
        window.location.href = "deliverRope.html";
    });

    //扫描领料单号
    $("#goods_id").on("keyup", function (event) {
        if (event.keyCode == "13") {
            var deliverRopeCode = $(this).val();
            if (!deliverRopeCode) return $alert("发货单号不可为空");
            getSaleoutorders(deliverRopeCode);
        }
    });

    //选择客户
    $(".searchbar .selectCustomerName").on("change", function () {
        if ($(".searchbar .selectCustomerName").val()) {
            var length = 0;
            for (var i = 0; i < $("#work_list li").length; i++) {
                if ($("#work_list li").eq(i).find("span.goods_customerName").text() == $(".searchbar .selectCustomerName").val()) {
                    length++;
                    $("#work_list li").eq(i).show();
                } else {
                    $("#work_list li").eq(i).hide();
                }
            }
            $(".content div.ul_foot span.sp_js").html(length);
        }
    })

    //获取发货单列表
    getSaleoutorders();

    function getSaleoutorders(deliverRopeCode) {
        //加载提示符 显示
        $(".content .list-block ul#work_list .infinite-scroll-preloader").show();
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/deliver/getRopeSaleoutordersNew',
            data: {
                deliverCode: deliverRopeCode
            },
            dataType: 'json',
            json: 'callback',
            cache: false,
            timeout: 60000,
            success: function (res) {
                if (res.success) {
                    var data = res.result;
                    //清空页面中的发货单
                    $("#work_list").find("li").remove();
                    //隐藏提示符 显示
                    $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                    var data_html = '';
                    var allCustomer = [];
                    for (var i = 0; i < data.length; i++) {
                        if (allCustomer.indexOf(data[i].customerName) == -1) {
                            allCustomer.push(data[i].customerName);
                        }
                        data_html += '<li>' +
                            '<p class="li_title">发货单号：<span class="sp work_code">' + data[i].number + '</span><span class="icon iconfont icon-right pull-right"></span></p>' +
                            '<div class="row no-gutter li_content"><div class="col-100">' +
                            '<div class="details">' +
                            '<p><span>客户:</span><span class="sp goods_customerName">' + data[i].customerName + '</span></p>' +
                            '<p><span>业务员:</span><span class="sp goods_businessUser">' + (data[i].businessUser ? data[i].businessUser : "") + '</span></p>' +
                            '<p><span>时间:</span><span class="sp goods_createTime">' + data[i].createTime + '</span></p>';
                        if (data[i].carNumber) {
                            data_html += '<p><span>车号/箱号:</span><span class="sp goods_carNumber">' + data[i].carNumber + '</span></p>';
                        }
                        if (data[i].warehouseName) {
                            data_html += '<p><span>仓库:</span><span class="sp goods_carNumber">' + data[i].warehouseName + '</span></p>';
                        }
                        if (data[i].comment) {
                            data_html += '<p><span>备注:</span><span class="sp comment">' + data[i].comment + '</span></p>';
                        }
                        data_html += '</div></div></div></li>';
                    }
                    $("#work_list").append(data_html);
                    //改变 共计 数
                    $(".content div.ul_foot span.sp_js").html($("#work_list li").length);
                    //
                    var selectCustomerHtml = '<option value="">请选择发货客户</option>';
                    for (var j = 0; j < allCustomer.length; j++) {
                        selectCustomerHtml += '<option value=' + allCustomer[j] + '>' + allCustomer[j] + '</option>';
                    }
                    $(".searchbar .selectCustomerName").html(selectCustomerHtml);
                } else {
                    $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                    $alert(res.message);
                }
            },
            error: function (error) {
                $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                if (error.status == 0) {
                    //loading_hide();
                    $alert("请求超时，请重新操作！");
                } else {
                    //loading_hide();
                    $alert(error.responseText);
                }
            }
        });
    }

    //点击 发货单箭头
    $(".content #work_list").on("click", "li .li_title", function () {
        var code = $(this).parent().find("span.work_code").html();
        localStorage.setItem("deliverRopeCode", JSON.stringify(code));
        window.location.href = "deliverRope_one.html";
    });

});